#include "linklist.h"
#include "ui_linklist.h"

#include <QFile>
#include <QMessageBox>


void LinkList::sendSlot()
{
    emit doubleCLicked();
}

int LinkList::getLastTime()
{
    return this->lastTime;
}

void LinkList::setLists(QStringList list)
{
    this->lists = list;
}

void LinkList::createTable()
{
    ui->tableWidget_LinkList->setRowCount(lists.size() / 6);
    int rowCount = 0;
    for(int i = 0; i < lists.size(); i += 6) {
        QDateTime startTime = QDateTime::fromString(lists[i+1],"hh:mm M月-dd日-yyyy");
        QDateTime endTime = QDateTime::fromString(lists[i+2],"hh:mm M月-dd日-yyyy");
        bool isTrue =  ((startTime <= QDateTime::currentDateTime()) && (endTime >= QDateTime::currentDateTime()));
        if(isTrue)
        {
            ui->tableWidget_LinkList->setItem(rowCount,0,new QTableWidgetItem(lists[i]));
            ui->tableWidget_LinkList->setItem(rowCount,1,new QTableWidgetItem(startTime.toString("M月dd日hh:mm")
                                     + " - " + endTime.toString("M月dd日hh:mm")));
            rowCount++;
        }

    }
    ui->tableWidget_LinkList->setRowCount(rowCount);
    //居中显示
    for (int i=0;i<rowCount;i++)
       {
           for (int j=0;j<2;j++)
           {
               ui->tableWidget_LinkList->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
           }
       }
}

LinkList::LinkList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LinkList)
{
    ui->setupUi(this);
    //设置列宽为自适应
    ui->tableWidget_LinkList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_LinkList->horizontalHeader()->setSectionResizeMode(0,QHeaderView::ResizeToContents);
    ui->tableWidget_LinkList->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选中模式为选中行
    ui->tableWidget_LinkList->setSelectionMode(QAbstractItemView::SingleSelection);  //一次只能选一行
    connect(ui->btn_select,&QPushButton::clicked,this,&LinkList::sendSlot);
    //加载样式表
    QString styleSheetFile = ":/new/prefix2/StyleSheet/linklistStyle.qss";
    QFile file(styleSheetFile);
    file.open(QFile::ReadOnly);
    if (file.isOpen())
    {
        QString styleSheet = this->styleSheet();
        styleSheet += QLatin1String(file.readAll());//读取样式表文件
        this->setStyleSheet(styleSheet);
        file.close();
    }
//    else
//    {
//        QMessageBox::information(this,"tip","cannot find qss file");
//    }
}

LinkList::~LinkList()
{
    delete ui;
}


void LinkList::on_btn_exit_clicked()
{
    this->close();
}

void LinkList::on_btn_select_clicked()
{
    QList<QTableWidgetItem*>items=ui->tableWidget_LinkList->selectedItems();
    if(items.isEmpty())
    {
        QMessageBox::information(nullptr,"warning","未选中任何内容");
        lastTime = 0;
        return;
    }
    int row = items[0]->row();
    QString time = ui->tableWidget_LinkList->item(row, 1)->text().right(5);
    QTime endTime = QTime::fromString(time,"hh:mm");
    QTime now = QTime::currentTime();
    int hour = endTime.hour() - now.hour();
    int min = endTime.minute() - now.minute();

    lastTime = hour * 60 + min;
    if(lastTime < 0)
    {
        QMessageBox::information(nullptr,"warning","当前计划已结束");
        lastTime = 0;
    }
    this->close();
}
